package org.jeecg.modules.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.erp.base.entity.ErpWarehouse;
import org.jeecg.modules.erp.base.service.IErpWarehouseService;
import org.jeecg.modules.inventory.entity.BussInventoryDetails;
import org.jeecg.modules.inventory.mapper.BussInventoryDetailsMapper;
import org.jeecg.modules.inventory.mapper.BussInventoryInoutDetailsMapper;
import org.jeecg.modules.inventory.service.IBussInventoryDetailsService;
import org.jeecg.modules.inventory.vo.BussInventoryDetailsPage;
import org.jeecg.modules.inventory.vo.BussInventoryDetailsPageVo;
import org.jeecg.modules.inventory.vo.BussInventoryDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 库存明细
 * @Author: jeecg-boot
 * @Date:   2024-07-01
 * @Version: V1.0
 */
@Service
public class BussInventoryDetailsServiceImpl extends ServiceImpl<BussInventoryDetailsMapper, BussInventoryDetails> implements IBussInventoryDetailsService {
	
	@Autowired
	private BussInventoryDetailsMapper bussInventoryDetailsMapper;
	@Autowired
	private IErpWarehouseService erpWarehouseService;
	@Autowired
	private BussInventoryInoutDetailsMapper bussInventoryInoutDetailsMapper;
	
	@Override
	public List<BussInventoryDetails> selectByMainId(String warehouseId,String materialId) {
		return bussInventoryDetailsMapper.selectByMainId(warehouseId,materialId);
	}

	@Override
	public IPage<BussInventoryDetails> myPage(Page<BussInventoryDetails> page, QueryWrapper<BussInventoryDetails> queryWrapper) {
		return bussInventoryDetailsMapper.myPage(page,queryWrapper);
	}

	@Override
	public IPage<BussInventoryDetailsPageVo> deductPage(Page<BussInventoryDetailsPageVo> page, QueryWrapper<BussInventoryDetailsPageVo> queryWrapper) {
		return bussInventoryDetailsMapper.deductPage(page,queryWrapper);
	}

	@Override
	public IPage<BussInventoryDetailsVo> queryWarehouseList(Page<BussInventoryDetailsVo> page, QueryWrapper<BussInventoryDetailsVo> queryWrapper) {
		return bussInventoryDetailsMapper.queryWarehouseList(page,queryWrapper);
	}

	@Override
	public void saveMain(BussInventoryDetailsPage bussInventoryDetailsPage) {
		//存在明细是拆分操作，不存在是修改库位
		if (bussInventoryDetailsPage.getBussInventoryDetails() != null && bussInventoryDetailsPage.getBussInventoryDetails().size()>0){

			List<BussInventoryDetails> detailsList = bussInventoryDetailsPage.getBussInventoryDetails();
			//删除原数据
			bussInventoryDetailsMapper.deleteByMainId(bussInventoryDetailsPage.getId());
			//存在明细说明是拆分
			for (BussInventoryDetails details : detailsList){
				ErpWarehouse warehouse = erpWarehouseService.selectByLocation(details.getWarehouseLocation());
				BussInventoryDetails bussInventoryDetails = bussInventoryDetailsMapper.selectById(bussInventoryDetailsPage.getId());
				BigDecimal oldQuantity = bussInventoryDetails.getQuantity();
				bussInventoryDetails.setId(null);
				bussInventoryDetails.setInventoryQuantity(details.getQuantity());
				bussInventoryDetails.setQuantity(details.getQuantity());
				bussInventoryDetails.setWarehouseLocation(details.getWarehouseLocation());
				bussInventoryDetails.setWarehouseLocationCode(details.getWarehouseLocation());
				bussInventoryDetails.setWarehouseCode(warehouse.getCode());
				bussInventoryDetails.setWarehouseName(warehouse.getName());

			}
			bussInventoryDetailsMapper.insertBatchSomeColumn(detailsList);


		}else{
			ErpWarehouse warehouse = erpWarehouseService.selectByLocation(bussInventoryDetailsPage.getWarehouseLocationCode());
			//修改库位
			BussInventoryDetails bussInventoryDetails = bussInventoryDetailsMapper.selectById(bussInventoryDetailsPage.getId());
			bussInventoryDetails.setWarehouseLocation(bussInventoryDetailsPage.getWarehouseLocationCode());
			bussInventoryDetails.setWarehouseLocationCode(bussInventoryDetailsPage.getWarehouseLocationCode());
			bussInventoryDetails.setWarehouseCode(warehouse.getCode());
			bussInventoryDetails.setWarehouseName(warehouse.getName());
			bussInventoryDetailsMapper.updateById(bussInventoryDetails);
		}
	}

	@Override
	public IPage<BussInventoryDetailsVo> selectKC(Page<BussInventoryDetails> page, QueryWrapper<BussInventoryDetails> queryWrapper) {
		return this.bussInventoryDetailsMapper.selectKC(page,queryWrapper);
	}
}
